<template>
  <el-dialog
    title="添加商品类别"
    :visible="goodsTypeVisible"
    @update:visible="v=>$emit('update:goodsTypeVisible',false)"
  >
    <el-form>
      <el-form-item label="商品类别名称:" label-width="120px">
        <el-input v-model="goodsTypeForm.goodsTypeName" autocomplete="off" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item label="所属店铺" label-width="120px">
        <el-select @change="fn" v-model="goodsTypeForm.shopTypeId" placeholder="请选择店铺类别">
          <el-option
            v-for="item in $store.state.shop.allShopTypeList"
            :key="item._id"
            :label="item.shopTypeName"
            :value="item._id"
          ></el-option>
        </el-select>
        <el-select v-model="goodsTypeForm.shopId" placeholder="请选择店铺">
          <el-option
            v-for="item in $store.state.shop.shopListByTypeId"
            :key="item._id"
            :label="item.shopName"
            :value="item._id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:goodsTypeVisible',false)">取 消</el-button>
      <el-button type="primary" @click="addGoodsTypeList">提 交</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  components: {},
  props: {
    goodsTypeVisible: {
      type: Boolean,
      defalut: false
    },
    shopId: {
      type: String,
      default: ""
    },
    shopTypeId: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      goodsTypeForm: {
        goodsTypeName: "",
        shopTypeId: "",
        shopId: ""
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    fn() {
      this.goodsTypeForm.shopId = "";
      this.$store.dispatch(
        "getShopListByShopTypeId",
        this.goodsTypeForm.shopTypeId
      );
    },
    addGoodsTypeList() {
      this.$store
        .dispatch("addGoodsTypeList", this.goodsTypeForm)
        .then(data => {
          if (data.ok === 1) {
            this.$emit("update:goodsTypeVisible", false);
            this.$store.dispatch("getGoodsTypeList");
            if (this.$route.name !== "/goodsTypeList") {
              this.$router.history.push("/goodsTypeList");
            } else {
              this.$message.error(data.msg);
            }
          }
        });
    }
  },
  created() {},
  mounted() {
    if (this.$store.state.shop.allShopTypeList.length < 1) {
      this.$store.dispatch("allShopTypeList");
    }
    if (this.shopTypeId) {
      this.goodsTypeForm.shopTypeId = this.shopTypeId;
      if (this.shopId) {
        this.$store.dispatch(
          "getShopListByShopTypeId",
          this.goodsTypeForm.shopTypeId
        );
        this.goodsTypeForm.shopId = this.shopId;
      }
    }
  }
};
</script>
<style lang="scss" scoped>
</style>